﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entitty;
using ClassLibrary.DAO;
using System.IO;

public partial class Admin_ucControl_ucNoidung : System.Web.UI.UserControl
{
    TblNoiDungDAO noidungDAO = new TblNoiDungDAO(Global.CONNECTION_STRING);
    TblImageDAO imageDAO = new TblImageDAO(Global.CONNECTION_STRING);
    TblMenuDAO menuDAO = new TblMenuDAO(Global.CONNECTION_STRING);


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Load_Menu();

            string id = Request.QueryString["id"];
            if (id != null)
            {
                LoadImageById();
                LoadDetail();
            }
        }
    }


    private void LoadDetail()
    {
        string id = Request.QueryString["id"];
        TblNoiDung noidung = noidungDAO.GetNoiDung_ByID(Global.ConvertToInt(id), 1);
        drlang.SelectedValue = noidung.IDLang.ToString();
        edNoidung.Content = noidung.Noidung;
    }
    // Load menu

    private void Load_Menu()
    {
        try
        {
            drMenu.DataSource = menuDAO.GetAll_Admin();
            drMenu.DataTextField = "MenuName";
            drMenu.DataValueField = "ID";
            drMenu.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    // Load Image
    private void LoadImageById()
    {
        string id = Request.QueryString["id"];
        grdImgItems.DataSource = imageDAO.GetImgItems(Global.ConvertToInt(id));
        grdImgItems.DataBind();
    }

    protected void grdImgItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    ImageButton button = control as ImageButton;
                    if (button != null && button.CommandName == "Delete")
                    {
                        // Add delete confirmation
                        button.CommandArgument = ((TblImage)e.Row.DataItem).ID.ToString();
                        button.OnClientClick = "if (!confirm('Are you sure to delete this Gallery?')) return false;";
                    }
                }
            }
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {

            string path = Server.MapPath("~\\Admin\\images\\Items\\");
            string filename = id + "_" + fileUpload.FileName;

            TblImage imgItem = new TblImage();
            // lblMessage.Text = path;
            if (File.Exists(path + "\\" + filename))
            {
                filename = DateTime.Now.ToString("mm") + "_" + DateTime.Now.ToString("ss") + "_" + filename;
            }
            else
            {
                filename = DateTime.Now.ToString("mm") + "_" + DateTime.Now.ToString("ss") + "_" + filename;
            }
            fileUpload.SaveAs(path + "\\" + filename);
            imgItem.ImageURL = filename;
            imgItem.IdNoidung = Convert.ToInt32(id);

            imageDAO.InsImage(imgItem, 1);
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            Response.Redirect("~/Admin/noi-dung-tieu-de-trang.aspx");
        }
    }
    // Xoá Image
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblImage imgItem = new TblImage();

            imgItem.ID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            imgItem = imageDAO.GetImageByID(Global.ConvertToInt(id), idXoa);
            imageDAO.DeleteImgItem(imgItem);

            string filename = imgItem.ImageURL;
            if (File.Exists(Server.MapPath("~\\Admin\\images\\Items\\" + filename)))
            {
                File.Delete(Server.MapPath("~\\Admin\\images\\Items\\" + filename));
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            LoadImageById();
        }
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            string id = Request.QueryString["id"];
            TblNoiDung noidung = new TblNoiDung();
            noidung.Noidung = Server.HtmlDecode(edNoidung.Content);
            noidung.IDLang = Global.ConvertToInt(drlang.SelectedValue);
            noidung.IDMenu = Global.ConvertToInt(drMenu.SelectedValue);


            if (id == "" || id == null)
            {
                noidung.IdNoidung = 0;
                noidungDAO.Insert(noidung, 1);
            }
            else
            {
                noidung.IdNoidung = Global.ConvertToInt(id);
                noidungDAO.Insert(noidung, 2);
            }
            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Response.Redirect("~/Admin/noi-dung-tieu-de-trang.aspx");
        }
    }
}
